package com.primecodegen.common.DmPrjProp;

import com.primecodegen.common.ProjectProperties.ProjectPropertiesDAO;
import com.primecodegen.common.ProjectProperties.ProjectPropertiesTable;
import com.primecodegen.common.DataModelProject.DataModelProjectDAO;
import com.primecodegen.common.DataModelProject.DataModelProjectTable;
import com.primecodegen.util.*;
import com.primecodegen.common.base.BaseBean;

import java.io.Serializable;
import java.util.*;
import java.util.ArrayList;
import java.util.logging.*;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import javax.servlet.http.HttpSession;

@ManagedBean(name = "dmPrjPropBean")
@RequestScoped
public class DmPrjPropBean extends BaseBean implements Serializable {

    // *********** Local Variables/Objects ***********
    private static final Logger logger = Logger.getLogger(DmPrjPropBean.class.getName());
    private int count;
    private int dmPrjPropID;
    private String excludedClmn;
    private DmPrjPropTable selectedDmPrjProp;
    HttpSession session;
    private DmPrjPropDAO dmPrjPropDAO;
    private ArrayList<DmPrjPropTable> dmPrjPropList;
    private DmPrjPropTable dmPrjPropSrchCrit;
    private DmPrjPropTable dmPrjPropUpdate;
    private DmPrjPropTable dmPrjPropTable;
    FacesContext context;
    ColToOrderBy colToOrderBy;

    // *********** Constructors ***********
    public DmPrjPropBean() {
        logger.info(" Constructor DmPrjPropBean()");
        excludedClmn = "6";
        context = getContext();
        session = getSession();
        dmPrjPropDAO = new DmPrjPropDAO();
        dmPrjPropList = new ArrayList<DmPrjPropTable>();
        dmPrjPropTable = new DmPrjPropTable();
        dmPrjPropSrchCrit = (DmPrjPropTable) session.getAttribute("sessionDmPrjPropSrchCrit");
        dmPrjPropUpdate = (DmPrjPropTable) session.getAttribute("dmPrjPropUpdateSess");
        colToOrderBy = (ColToOrderBy) session.getAttribute("sessionColToOrderBy");

        if (dmPrjPropSrchCrit == null) {
            dmPrjPropSrchCrit = new DmPrjPropTable();
        }

        if (dmPrjPropUpdate == null) {
            dmPrjPropUpdate = new DmPrjPropTable();
        }



        fillDmPrjPropList();
    }

    // *********** Setters & Getters ***********
    public String getExcludedClmn() {
        return excludedClmn;
    }

    public void setExcludedClmn(String excludedClmn) {
        this.excludedClmn = excludedClmn;
    }

    public DmPrjPropTable getDmPrjPropUpdate() {
        dmPrjPropUpdate = (DmPrjPropTable) session.getAttribute("dmPrjPropUpdateSess");
        if (dmPrjPropUpdate == null) {
            dmPrjPropUpdate = new DmPrjPropTable();
        }
        return dmPrjPropUpdate;
    }

    public void setDmPrjPropUpdate(DmPrjPropTable dmPrjPropUpdate) {
        session.setAttribute("dmPrjPropUpdateSess", dmPrjPropUpdate);
        this.dmPrjPropUpdate = dmPrjPropUpdate;
    }

    public DmPrjPropBean(DmPrjPropTable dmPrjPropTable) {
        logger.info("Constructor DmPrjPropBean( DmPrjPropTable dmPrjPropTable )");
        this.dmPrjPropTable = dmPrjPropTable;
    }

    public DmPrjPropTable getDmPrjPropTable() {
        return dmPrjPropTable;
    }

    public void setDmPrjPropTable(DmPrjPropTable dmPrjPropTable) {
        this.dmPrjPropTable = dmPrjPropTable;
    }

    public DmPrjPropTable getDmPrjPropSrchCrit() {
        return dmPrjPropSrchCrit;
    }

    public void setDmPrjPropSrchCrit(DmPrjPropTable dmPrjPropSrchCrit) {
        this.dmPrjPropSrchCrit = dmPrjPropSrchCrit;
    }

    public DmPrjPropTable getSelectedDmPrjProp() {
        return selectedDmPrjProp;

    }

    public void setSelectedDmPrjProp(DmPrjPropTable selectedDmPrjProp) {
        this.selectedDmPrjProp = selectedDmPrjProp;
    }

    public ArrayList<DmPrjPropTable> getDmPrjPropList() {
        return dmPrjPropList;
    }
//******************************
    private ArrayList<SelectItem> dataModelProjectList = new ArrayList<SelectItem>();
    private DataModelProjectDAO dataModelProjectDAO = new DataModelProjectDAO();

    public ArrayList<SelectItem> getDataModelProjectList() {
        getAllDataModelProject();
        return dataModelProjectList;
    }

    public void setDataModelProjectList(ArrayList<SelectItem> dataModelProjectList) {
        this.dataModelProjectList = dataModelProjectList;
    }

    public void getAllDataModelProject() {
        dataModelProjectList = new ArrayList<SelectItem>();
        DataModelProjectTable groupTable = new DataModelProjectTable();
        dataModelProjectList.add(new SelectItem("", "- Select Role -"));
        try {
            Iterator itDataModelProject = dataModelProjectDAO.getByCriterionOrderedBy(null, null).iterator();
            while (itDataModelProject.hasNext()) {
                groupTable = (DataModelProjectTable) itDataModelProject.next();
                dataModelProjectList.add(new SelectItem(groupTable.getID(), groupTable.getCode()));
                groupTable = new DataModelProjectTable();
            }

        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in public void getAllDataModelProject() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }
    //******************************



//******************************
    private ArrayList<SelectItem> projectPropertiesList = new ArrayList<SelectItem>();
    private ProjectPropertiesDAO projectPropertiesDAO = new ProjectPropertiesDAO();

    public ArrayList<SelectItem> getProjectPropertiesList() {
        getAllProjectProperties();
        return projectPropertiesList;
    }

    public void setProjectPropertiesList(ArrayList<SelectItem> projectPropertiesList) {
        this.projectPropertiesList = projectPropertiesList;
    }

    public void getAllProjectProperties() {
        projectPropertiesList = new ArrayList<SelectItem>();
        ProjectPropertiesTable groupTable = new ProjectPropertiesTable();
        projectPropertiesList.add(new SelectItem("", "- Select Role -"));
        try {
            Iterator itProjectProperties = projectPropertiesDAO.getByCriterionOrderedBy(null, null).iterator();
            while (itProjectProperties.hasNext()) {
                groupTable = (ProjectPropertiesTable) itProjectProperties.next();
                projectPropertiesList.add(new SelectItem(groupTable.getID(), groupTable.getCode()));
                groupTable = new ProjectPropertiesTable();
            }

        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in public void getAllProjectProperties() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }
    //******************************

    // *********** Actions ***********
    public void doFilter() {
        logger.log(Level.INFO, "Starting: public void doFilter () in DmPrjPropBean");
        try {
            dmPrjPropSrchCrit.setFilter(true);
            this.dmPrjPropList = dmPrjPropDAO.getByCriterionOrderedBy(dmPrjPropSrchCrit, null);

            session.setAttribute("sessionDmPrjPropSrchCrit", dmPrjPropSrchCrit);
            fillDmPrjPropList();
            logger.log(Level.INFO, "Finishing: public void doFilter () in DmPrjPropBean");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in doFilter() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }

    public void clearFilter() {
        try {
            dmPrjPropSrchCrit = null;
            session.setAttribute("sessionDmPrjPropSrchCrit", dmPrjPropSrchCrit);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sortByID() {

        logger.log(Level.INFO, "public void fillDmPrjPropList ()");

        try {

            if (dmPrjPropSrchCrit != null && colToOrderBy != null) {
                this.dmPrjPropList = dmPrjPropDAO.getByCriterionOrderedBy(dmPrjPropSrchCrit, colToOrderBy);
            } else if (colToOrderBy != null) {
                this.dmPrjPropList = dmPrjPropDAO.getByCriterionOrderedBy(null, colToOrderBy);
            } else if (dmPrjPropSrchCrit != null) {
                this.dmPrjPropList = dmPrjPropDAO.getByCriterionOrderedBy(dmPrjPropSrchCrit, null);
            } else {
                this.dmPrjPropList = dmPrjPropDAO.getByCriterionOrderedBy(null, null);
            }




        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in public void fillDmPrjPropList () . Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
        }
    }

    public void update() {
        try {
            int parID = new Integer(session.getAttribute("dmPrjPropIDSess").toString());
            dmPrjPropUpdate.setID(parID);
            dmPrjPropDAO.updateByID(dmPrjPropUpdate);

        } catch (Exception e) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure",
                    "Sorry!! an exception occured interface the update action, the webmaster is notified and if action required it will be taken shortly"));
            logger.log(Level.WARNING, "Exception occured in Update(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});

        }
    }

    public void Insert() {
        logger.info("public void Insert()");
        try {

            int isInserted = -1;


            isInserted = dmPrjPropDAO.insert(dmPrjPropTable);

            if (isInserted != -1) {
                getContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Saved Successfully", "DmPrjProp Added"));
                fillDmPrjPropList();
            } else {
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "error", "error shit"));
                logger.log(Level.WARNING, "Exception occured in Insert(). Error Message: isInserted != -1 Probably exception occure in dmPrjPropDAO.insert( dmPrjPropTable )");

            }

        } catch (Exception e) {

            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure",
                    "Sorry!! an exception occured, the webmaster is notified and if action required it will be taken shortly"));
            logger.log(Level.WARNING, "Exception occured in delete(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});

        }
    }

    public void delete() {
        try {

            int parID = new Integer(session.getAttribute("dmPrjPropIDSess").toString());



            dmPrjPropDAO.deleteByID(parID);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "DmPrjProp Deleted"));
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in delete(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure", "Sorry!! an exception occured, the webmaster is notified and if action required it will be taken shortly"));
        }
    }

    public void setIDinSession() {
        try {
            Map<String, String> params = context.getExternalContext().getRequestParameterMap();
            int parID = new Integer(params.get("dmPrjPropID"));
            dmPrjPropUpdate = (DmPrjPropTable) dmPrjPropDAO.getByID(parID);
            session.setAttribute("dmPrjPropUpdateSess", dmPrjPropUpdate);
            session.setAttribute("dmPrjPropIDSess", parID);
        } catch (Exception ex) {
            Logger.getLogger(DmPrjPropBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void fillDmPrjPropList() {
        logger.log(Level.INFO, "Starting: public void fillDmPrjPropList () in DmPrjPropBean");
        try {

            if ((dmPrjPropSrchCrit != null && dmPrjPropSrchCrit.isFilter()) && colToOrderBy != null) {
                this.dmPrjPropList = dmPrjPropDAO.getByCriterionOrderedBy(dmPrjPropSrchCrit, colToOrderBy);
            } else if (colToOrderBy != null) {
                this.dmPrjPropList = dmPrjPropDAO.getByCriterionOrderedBy(null, colToOrderBy);
            } else if (dmPrjPropSrchCrit != null && dmPrjPropSrchCrit.isFilter()) {
                this.dmPrjPropList = dmPrjPropDAO.getByCriterionOrderedBy(dmPrjPropSrchCrit, null);
            } else {
                this.dmPrjPropList = dmPrjPropDAO.getByCriterionOrderedBy(null, null);
            }



            logger.log(Level.INFO, "finishing: public void fillDmPrjPropList () in DmPrjPropBean");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in public void fillDmPrjPropList () . Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
        }
    }
}
